<template>
    <div>
        <el-dialog :title="addInfo.isAdd ? '添加商品规格' : '编辑商品规格'" :visible.sync="addInfo.isShow" :before-close="cancel"
            center>
            <el-form :model="specs_info" :rules="rules" ref="specs_info">
                <el-form-item label="规格名称" :label-width="formLabelWidth" prop="specsname">
                    <el-input v-model="specs_info.specsname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="规格属性" :label-width="formLabelWidth" v-for="(item, index) in attrArr">
                    <el-input class="inpinfo" v-model="item.value" autocomplete="off"></el-input>
                    <el-button v-if="index == 0" @click="add_Info" type="primary">新增规格属性</el-button>
                    <el-button @click="del_info(index)" v-else type="danger">删除</el-button>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="specs_info.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2"></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button @click="confirn" type="primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
import { specsadd,specsedit,specsinfo } from '@/request/api'
export default {
    props: ['addInfo'],
    data() {
        return {
            specs_info: {
                specsname: '',//商品规格名称
                attrs: '',//商品规格属性值  1,2,4
                status: 1//状态1正常2禁用
            },
            formLabelWidth: '120px',
            rules: {
                specsname: [
                    { required: true, message: "请输入商品规格名称", trigger: "blur" },
                    { min: 2, max: 15, message: "长度在 2 到 15 个字符", trigger: "blur" },
                ]
            },
            //定义一个数组 用于循环遍历动态表单项
            attrArr: [
                { value: '' }//input的双向数据绑定内容
            ]
        }
    },
    methods: {
        //封装一个动态添加表单项的事件
        add_Info() {
            //对添加的表单项进行一个控制
            if (this.attrArr.length > 4) {
                this.$message.warning("最多添加4项")
            } else {
                this.attrArr.push({
                    value: ''
                })
            }
        },
        //动态删除表单项
        del_info(n) {
            this.attrArr.splice(n, 1)
        },
        ...mapActions({
            getSpecs: 'specs/getSpecslistAction',
            getCount: 'specs/getCountAction'
        }),
        //封装一个取消事件
        cancel(e) {
            //去改变父组件的isShow的状态
            this.$emit('cancel', false);
            this.reset()
        },
        //封装一个清空事件
        reset() {
            this.specs_info = {
                specsname: '',//商品规格名称
                attrs: '',//商品规格属性值  1,2,4
                status: 1//状态1正常2禁用
            }
            //调用清空验证的方法
            this.$refs.specs_info.resetFields();
            //清空动态表单项
            this.attrArr = [{ value: '' }]
        },
        //封装一个查询一条数据的接口
        lookup(id) {
            specsinfo({ id }).then(res => {
                if (res.code == 200) {
                    this.specs_info = res.list[0];
                    this.specs_info.id = id;
                    this.specs_info.attrs.map((item, index) => {
                        if (index == 0) {
                            this.attrArr[0].value = item
                        } else {
                            this.attrArr.push({
                                value: item
                            })
                        }
                    })
                }
            })
        },
        // 确定事件
        confirn() {
            this.$refs.specs_info.validate((val) => {
                if (val) {
                    this.specs_info.attrs = this.attrArr.map((item) => item.value).join(',');
                    if (this.addInfo.isAdd) {
                        specsadd(this.specs_info).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                this.cancel();
                                this.getSpecs();
                                this.getCount();
                            } else {
                                this.$message(res.msg)
                            }
                        })
                    } else {
                        specsedit(this.specs_info).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                this.cancel();
                                this.getSpecs();
                                this.getCount();
                            } else {
                                this.$message.error(res.error)
                            }
                        })
                    }
                }else{
                    this.$message.error('验证失败');
                    return false
                }
            })
        }
    }
}
</script>

<style scoped>
.inpinfo{
    width: 70%;
}
</style>